package com.swt.gap.common.file.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.*;
import com.swt.gap.common.file.File;
import com.swt.gap.common.file.FormFileData;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/7/29 11:17
 */
public class CsvFile implements File {

    private static final CsvReadConfig CSV_READ_CONFIG = new CsvReadConfig();
    static {
        CSV_READ_CONFIG.setContainsHeader(true);
    }

    @Override
    public FormFileData readFormFile(String filePath) {

        CsvReader reader = CsvUtil.getReader(CSV_READ_CONFIG);
        CsvData data = reader.read(FileUtil.file(filePath), StandardCharsets.UTF_8);
        List<String> header = data.getHeader();
        List<Map<String, String>> rowsData = new ArrayList<>();
        List<CsvRow> csvRows = data.getRows();
        for (CsvRow csvRow : csvRows) {
            Map<String, String> rowData = csvRow.getFieldMap();
            rowsData.add(rowData);
        }
        return new FormFileData(header, rowsData);
    }
}
